/*
 * Sym2x2XMLConfigFileParameters.java
 *
 * Created on 29 de Março de 2010, 18:26
 *
 */

package intopo.game.impl.sym2x2;

import intopo.game.XMLConfigFileParameters;

import util.xml.*;

import cern.jet.random.engine.RandomEngine;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.HashMap;

/**
 *
 * @author <a href="mailto:plsm@ua.pt">Pedro Lopes da Silva Mariano</a>
 *
 * @version 1.0 29 de Março de 2010
 */
final public class Sym2x2XMLConfigFileParameters
	implements XMLConfigFileParameters<Symmetric2x2Strategy, Symmetric2x2Game>
{
	final private static String ROOT_NODE = "Symmetric2x2Game";

	final private static Map<String, Constructor<? extends Symmetric2x2Strategy>> STRATEGY_CONSTRUCTORS;
	
	final private static Map<String, Constructor<? extends Symmetric2x2Game>> GAME_CONSTRUCTORS;

	static
	{
		Constructor<Symmetric2x2Strategy> cp;
		STRATEGY_CONSTRUCTORS = new HashMap<String, Constructor<? extends Symmetric2x2Strategy>> ();
		try {
			Class<Symmetric2x2Strategy> c = Symmetric2x2Strategy.class;
			cp = c.getConstructor (Element.class, RandomEngine.class);
			STRATEGY_CONSTRUCTORS.put ("Symmetric2x2Strategy", cp);
		}
		catch (NoSuchMethodException nsme) {
			System.err.println ("Class Symmetric2x2Strategy does not have a constructor with parameter type org.w3c.dom.Element");
			System.exit (1);
		}
		Constructor<Symmetric2x2Game> cg;
		GAME_CONSTRUCTORS = new HashMap<String, Constructor<? extends Symmetric2x2Game>> ();
		try {
			Class<Symmetric2x2Game> c = Symmetric2x2Game.class;
			cg = c.getConstructor (Element.class);
			GAME_CONSTRUCTORS.put ("Symmetric2x2Game", cg);
		}
		catch (NoSuchMethodException nsme) {
			System.err.println ("Class IteratedPrisonerDilemmaGame does not have a constructor with parameter type org.w3c.dom.Element");
			System.exit (1);
		}
	}

	/**
	 * Creates a new instance of Sym2x2XMLConfigFileParameters
	 */
	public Sym2x2XMLConfigFileParameters ()
{
	}

	public String getRootNodeName ()
	{
		return ROOT_NODE;
	}

	/**
	 * Return a map that associates tag names to game constructors. The XML file should only have a single tag whose name is a key of the returned map.
	 */
	public Map<String, Constructor<? extends Symmetric2x2Game>> getGameConstructor ()
	{
		return GAME_CONSTRUCTORS;
	}

	/**
	 * Return a map that associates tag names to strategy constructors.
	 */
	public Map<String, Constructor<? extends Symmetric2x2Strategy>> getStrategyConstructors ()
	{
		return STRATEGY_CONSTRUCTORS;
	}

	public void parse (Node rootNode)
		throws XMLParseException
	{
		AttributeParser<Integer> api;
		AttributeParser<Float> apf;
		
		api = new AttributeParser<Integer>
			(new CheckExistenceAttribute<Integer> (),
			 new ParseAsInt (0, Integer.MAX_VALUE));
		apf = new AttributeParser<Float>
			(new CheckExistenceAttribute<Float> (),
			 new ParseAsFloat (0, 1.0f));

		Node e1, e2;
		if ((e1 = XML.getChildNodeWithName (rootNode, "Sym2x2StrategyParameters")) != null) {
			Symmetric2x2Strategy.Parameters.probabilityBins = api.parse ((Element) e1, "probabilityBins");
			Symmetric2x2Strategy.Parameters.normalStandardDeviation = apf.parse ((Element) e1, "normalStandardDeviation");
		}
	}
}
